package lesson14.protobuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

/**
 * <br/>
 * Created on : 2018-02-28 23:08
 * @author lizebin
 */
public class ClientHandler extends SimpleChannelInboundHandler<Sinoxk.Message> {

	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Sinoxk.Message msg) throws Exception {

	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		int randomInt = new Random().nextInt(3);

		Sinoxk.Message.Builder messageBuilder = Sinoxk.Message.newBuilder();

		switch(randomInt) {
			case 0: {
				System.out.println("it is a person");
				messageBuilder.setDataType(Sinoxk.Message.DataType.PersonType)
					.setPerson(Sinoxk.Person.newBuilder().setAddress("address").setAge("27").setName("lizebin").build());
				break;
			}
			case 1: {
				System.out.println("it is a dog");
				messageBuilder.setDataType(Sinoxk.Message.DataType.DogType)
					.setDog(Sinoxk.Dog.newBuilder().setAge(2).setName("dogdog").build());
				break;
			}
			default : {
				System.out.println("it is a cat");
				messageBuilder.setDataType(Sinoxk.Message.DataType.CatType)
					.setCat(Sinoxk.Cat.newBuilder().setCity("city").setName("catcat").build());
			}
		}
		ctx.channel().writeAndFlush(messageBuilder.build());
	}
}
